package com.ruoyi.project.newhope.schedule;

import cn.hutool.core.date.DateUtil;
import com.ruoyi.project.newhope.data.domain.NewHopeData;
import com.ruoyi.project.newhope.data.mapper.NewHopeCollectDataMapper;
import com.ruoyi.project.newhope.setting.domain.NewHopeSetting;
import com.ruoyi.project.newhope.setting.mapper.NewHopeSettingMapper;
import com.ruoyi.project.newhope.warning.domain.NewHopeWarning;
import com.ruoyi.project.newhope.warning.mapper.NewHopeWarningMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;

/**
 * 数据备份任务
 */
@Slf4j
@Component
public class BackupsDataTask implements ScheduledOfTask {

    @Autowired
    private NewHopeSettingMapper settingMapper;
    @Autowired
    private NewHopeCollectDataMapper collectDataMapper;
    @Autowired
    private NewHopeWarningMapper warningMapper;

    private Integer executeCount = 0;

    @Override
    public void execute() {
        log.info("数据备份开始");
        log.info("thread id:{}, task execute times:{}", Thread.currentThread().getId());
        log.info("execute times = {}", executeCount++);
        log.info("execute date = {}", DateUtil.format(new Date(), "yyyy-MM-dd HH:mm:ss"));

        NewHopeSetting hopeSetting = settingMapper.selectNewHopeSettingById(2L);
        // 数据清理备份 1.周 2.月 3.季度 4.年
        if (hopeSetting.getValue() == null) {
            return;
        }
        int rate = Integer.parseInt(hopeSetting.getValue());
        Date targetDate = new Date();
        if (rate == 1) {
            targetDate = DateUtil.offsetWeek(targetDate, -1);
        } else if (rate == 2) {
            targetDate = DateUtil.offsetMonth(targetDate, -1);
        } else if (rate == 3) {
            targetDate = DateUtil.offsetMonth(targetDate, -3);
        } else if (rate == 4) {
            targetDate = DateUtil.offsetMonth(targetDate, -12);
        }

        log.info("execute query date = {}", targetDate);

        //查询备份数据-采集数据
        List<NewHopeData> data = collectDataMapper.selectDataByDate(targetDate);
        log.info("new_hope_collect_data backup nums = {}", data.size());
        if (CollectionUtils.isNotEmpty(data)) {
            //批量插入备份数据库-采集数据
            log.info("new_hope_collect_data backup start");
            Integer count = 0;
            for (NewHopeData hopeData : data) {
                try {
                    Integer result = collectDataMapper.insertBackup(hopeData);
                    count = count + result;
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
            }
            log.info("new_hope_collect_data backup end  count={}", count);
            //删除已经备份过的数据-采集数据
            Integer deleteCount = collectDataMapper.deleteDataByDate(targetDate);
            log.info("new_hope_collect_data backup end  deleteCount={}", deleteCount);
        }

        //查询备份数据-告警数据
        List<NewHopeWarning> warningData = warningMapper.selectDataByDate(targetDate);
        log.info("new_hope_warning backup nums = {}", data.size());
        if (CollectionUtils.isNotEmpty(data)) {
            //批量插入备份数据库-采集数据
            log.info("new_hope_warning backup start");
            Integer count = 0;
            for (NewHopeWarning warning : warningData) {
                try {
                    warningMapper.insertBackup(warning);
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
            }
            log.info("new_hope_warning backup end  count={}", count);
            //删除已经备份过的数据-采集数据
            Integer deleteCount = warningMapper.deleteDataByDate(targetDate);
            log.info("new_hope_warning backup end  deleteCount={}", deleteCount);
        }

        log.info("数据备份结束");
    }
}
